﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZY.Models;


namespace ZY.Controllers
{
    public class DefaultController : Controller
    {
        //数据上下文
        Model1 model = new Model1();


        //列表显示
        public ActionResult Index()
        {
            var list = model.Task.ToList();
            return View(list);
        }

        //详情
        public ActionResult Details(int id)
        {

            Task task = model.Task.FirstOrDefault(p => p.TaskID == id);


            return View(task);
        }

        [HttpPost]
        //删除
        public ActionResult Delete(int id)
        {
            Task task = model.Task.FirstOrDefault(p => p.TaskID == id);
            if (task != null)
            {
                model.Task.Remove(task);
                var result = model.SaveChanges() > 0;
                return Json(result);
            }
            return Json(false);
        }
        //修改
        public ActionResult Edit()
        {
            return View();
        }


        [HttpPost]
        public ActionResult Edit(Task task)
        {
            if (ModelState.IsValid)
            {
                var list = model.Task.Where(p => p.TaskID == task.TaskID).FirstOrDefault();

                if (list != null)
                {
                    list.TaskID = task.TaskID;
                    list.TaskPriority = task.TaskPriority;
                    list.Content = task.Content;
                    list.AddTime = task.AddTime;
                    list.FinishedTime = DateTime.Now;
                    list.uid = task.uid;
                    list.TaskState = task.TaskState;


                }
                var res = model.SaveChanges();
                if (res > 0)
                {
                    return RedirectToAction("Index");
                }

            }

            return View(task);
        }

        //添加
        public ActionResult Create()
        {
            return View();
        }


        [HttpPost]
        public ActionResult Create(Task task)
        {
            if (ModelState.IsValid)
            {


                model.Task.Add(task);
                var result = model.SaveChanges();
                if (result > 0)
                {
                    return RedirectToAction("Index");
                }
            }
            return View(task);
        }


    }
}
